package com.kldy8.kldyw.web;

import android.app.Fragment;
import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.kldy8.kldyw.Main2Activity;
import com.kldy8.kldyw.MyWebActivity;
import com.kldy8.kldyw.R;
import com.kldy8.kldyw.filmfactory.FilmTalent.FilmTalentActivity;
import com.kldy8.kldyw.filmfactory.company.CompanyActivity;
import com.kldy8.kldyw.filmfactory.dteam.DteamListActivity;
import com.kldy8.kldyw.filmfactory.equipment.DeviceActivity;
import com.kldy8.kldyw.filmfactory.shootingvenue.ShootingVenueActivity;
import com.kldy8.kldyw.system.UrlBuilder;

/**
 * Created by user on 2016/11/29.
 */

public abstract class BaseFragment  extends Fragment implements IWebLodingBiz
{
    private WebView webView;
    private String urlString;
    private String news_id;
    private Main2Activity activity;

    private RelativeLayout reloadlayout;
    private RelativeLayout loadinglayout;
    private ImageView reload_img;

    //初始化
    final void initView(View view,String title)
    {
        Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        toolbar.setTitle(getResources().getString(R.string.title));//设置主标题

        loadinglayout = (RelativeLayout) view.findViewById(R.id.loadinglayout);
        reloadlayout = (RelativeLayout) view.findViewById(R.id.reloadlayout);
        reload_img = (ImageView) view.findViewById(R.id.reload_img);
        reload_img.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                loadinglayout.setVisibility(View.VISIBLE);
                webView.loadUrl(urlString);
            }
        });

        webView = (WebView) view.findViewById(R.id.webView);
        WebSettings settings = webView.getSettings();
        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        settings.setJavaScriptEnabled(true);
        webView.loadUrl(urlString);
        webView.setWebViewClient(new WebViewClient()
        {
            @Override
            public void onReceivedError(WebView view, int errorCode,
                                        String description, String failingUrl)
            {
                // TODO Auto-generated method stub
//                super.onReceivedError(view, errorCode, description, failingUrl);
                loadinglayout.setVisibility(View.GONE);
                reloadlayout.setVisibility(View.VISIBLE);
            }
            @Override
            public void onPageFinished(WebView view, String url)
            {
                super.onPageFinished(view, url);
                loadinglayout.setVisibility(View.GONE);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                reloadlayout.setVisibility(View.GONE);
                Log.e("this is my Url",url);
                if (url.contains(AppWebUrl.URL_JOB_PERSON_LIST))
                {
                    Intent intent = new Intent();
                    intent.setClass(activity, FilmTalentActivity.class);
                    intent.putExtra("item",0);
                    activity.startActivityRightToLeft(intent);
                    view.loadUrl(urlString);
                    return true;
                }
                if (url.contains(AppWebUrl.URL_JOB_COMPANY_LIST))
                {
                    Intent intent = new Intent();
                    intent.setClass(activity, FilmTalentActivity.class);
                    intent.putExtra("item",1);
                    activity.startActivityRightToLeft(intent);
                    view.loadUrl(urlString);
                    return true;
                }
                if (url.contains(AppWebUrl.URL_DTEAM_DTEAM_LIST))
                {
                    Intent intent = new Intent();
                    intent.putExtra("item",0);
                    intent.setClass(activity, DteamListActivity.class);
                    activity.startActivityRightToLeft(intent);
                    view.loadUrl(urlString);
                    return true;
                }
                if (url.contains(AppWebUrl.URL_PLACE_PLACE_LIST))
                {
                    Intent intent = new Intent();
                    intent.putExtra("item",0);
                    intent.setClass(activity, ShootingVenueActivity.class);
                    activity.startActivityRightToLeft(intent);
                    view.loadUrl(urlString);
                    return true;
                }

                if (url.contains(AppWebUrl.URL_DEVICE_DEVICE_LIST))
                {
                    Intent intent = new Intent();
                    intent.putExtra("item",0);
                    intent.setClass(activity, DeviceActivity.class);
                    activity.startActivityRightToLeft(intent);
                    view.loadUrl(urlString);
                    return true;
                }
                if (url.contains(AppWebUrl.URL_COMPANY_COMPANY_LIST))
                {
                    String type = "15";
                    if (url.contains("type_id=15"))
                    {
                        type = "15";
                    }
                    if (url.contains("type_id=14"))
                    {
                        type = "14";
                    }
                    if (url.contains("type_id=16"))
                    {
                        type = "16";
                    }
                    if (url.contains("type_id=17"))
                    {
                        type = "17";
                    }
                    Intent intent = new Intent();
                    intent.putExtra("type_id",type);
                    intent.setClass(activity, CompanyActivity.class);
                    activity.startActivityRightToLeft(intent);
                    view.loadUrl(urlString);
                    return true;
                }
                else
                {
                    Intent intent = new Intent();
                    String resultUrl = AppWebUrl.createUrl(url);
                    intent.putExtra("url",resultUrl);
                    intent.setClass(activity, MyWebActivity.class);
                    activity.startActivityRightToLeft(intent);
                    view.loadUrl(urlString);
                    return true;
                }
            }
        });

    }


    private boolean containsUrl()
    {
        return  true;
    }


}
